package com.dp.mallchat.common.chat.consumer;

import com.dp.mallchat.common.chat.dao.ContactDao;
import com.dp.mallchat.common.chat.dao.MessageDao;
import com.dp.mallchat.common.chat.dao.RoomDao;
import com.dp.mallchat.common.chat.dao.RoomFriendDao;
import com.dp.mallchat.common.chat.domain.entity.Message;
import com.dp.mallchat.common.chat.domain.entity.Room;
import com.dp.mallchat.common.chat.domain.entity.RoomFriend;
import com.dp.mallchat.common.chat.domain.vo.resp.ChatMessageResp;
import com.dp.mallchat.common.chat.service.ChatService;
import com.dp.mallchat.common.chat.service.adapter.RoomAdapter;
import com.dp.mallchat.common.chat.service.cache.GroupMemberCache;
import com.dp.mallchat.common.chat.service.cache.HotRoomCache;
import com.dp.mallchat.common.chat.service.cache.RoomCache;
import com.dp.mallchat.common.chat.service.cache.RoomFriendCache;
import com.dp.mallchat.common.common.constant.MQConstant;
import com.dp.mallchat.common.common.domain.dto.MsgMessageSendDTO;
import com.dp.mallchat.common.user.service.adapter.WSAdapter;
import com.dp.mallchat.common.user.service.impl.PushService;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: dupeng
 * @CreateTime: 2024-07-05  11:03
 * @Description: 发送消息消费者
 */
@Component
@RocketMQMessageListener(consumerGroup = MQConstant.SEND_MSG_GROUP, topic = MQConstant.SEND_MSG_TOPIC, messageModel = MessageModel.BROADCASTING)
public class MessageConsumer implements RocketMQListener<MsgMessageSendDTO> {

    @Autowired
    private MessageDao messageDao;
    @Autowired
    private RoomCache roomCache;
    @Autowired
    private RoomDao roomDao;
    @Autowired
    private PushService pushService;
    @Autowired
    private ChatService chatService;
    @Autowired
    private GroupMemberCache groupMemberCache;
    @Autowired
    private RoomFriendCache roomFriendCache;
    @Autowired
    private HotRoomCache hotRoomCache;
    @Autowired
    private RoomFriendDao roomFriendDao;
    @Autowired
    private ContactDao contactDao;
    /**
     * 1、获取消息信息
     * 2、获取房间（缓存）
     * 3、消息对房间每一位用户的状态，我是否有点赞，是否有举报等信息-- todo
     * 4、更新房间最新消息状态
     * 5、删除房间缓存（roomId）
     * 6、是否为热门群聊
     * 是：
     * 更新热门群聊时间-redis(会话列表排序)todo
     * 推送所有人消息（3）
     * MQ广播到所有在线Map
     * 否：
     * 是否为群聊
     * 群聊获取群内所有用户id
     * 单条获取用户id
     * 更新所有群成员的会话时间
     * 推送房间成员MQ广播
     * <p>
     * <p>
     * 监听消息发送topic
     * 全员推送
     * 定向用户推送
     *
     * @param messageSendDTO
     */
    @Override
    public void onMessage(MsgMessageSendDTO messageSendDTO) {
        //获取消息
        Message message = messageDao.getById(messageSendDTO.getMsgId());
        //获取房间
        Room room = roomCache.get(message.getRoomId());
        //推送消息内容 ChatMessageResp
        ChatMessageResp chatMessageResp = chatService.getMsgResp(message, null);
        //更新房间最新一条消息id和最新消息时间，last_msg_id，active_time,TODO 可能存在消费顺序问题，先发的消息后进行消费，导致room最新id出错
        roomDao.updateById(RoomAdapter.buildUpdateRoom(room, message));

        //删除房间缓存
        roomCache.delete(message.getRoomId());
        //热门群聊
        if (room.isHotRoom()) {
            //redis更新会话排名 zset ,通过score排序
            hotRoomCache.refreshActiveTime(room.getId(), message.getCreateTime());
            //推送所有人消息
            pushService.sendPushMsg(WSAdapter.buildPushMsg(chatMessageResp));
        } else {
            //群聊与单聊
            List<Long> groupMember = new ArrayList<>();
            if (room.isGroupRoom()) {
                //群成员
                groupMember = groupMemberCache.getGroupMember(room.getId());
            } else if (room.isFriendRoom()) {
                //对单人推送
                RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
                groupMember = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());
            }
            //更新所有群成员的会话时间 redis
            contactDao.refreshOrCreateActiveTime(room.getId(), groupMember, message.getId(), message.getCreateTime());
            //房间好友推送
            pushService.sendPushMsg(WSAdapter.buildPushMsg(chatMessageResp), groupMember);
        }
    }
}
